package com.hlingsoft.kkh.action.v20;

import com.hlingsoft.kkh.action.BaseAction;
import com.hlingsoft.kkh.dao.*;
import com.hlingsoft.kkh.entity.*;

import com.hlingsoft.kkh.entity.vo.EventsSearchVo;
import com.hlingsoft.kkh.entity.vo.EventsVo1;
import com.hlingsoft.kkh.entity.vo.StartsVo;
import com.hlingsoft.kkh.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by selbylei on 2015/6/15.
 */
@Controller
@RequestMapping("/v20/search")
public class SearchAction extends BaseAction {



    /**
     * 搜索列表
     * @param content
     * @param type
     * @param request
     * @return
     */
    @RequestMapping(value = "/list.json")
    @ResponseBody
    public Map<String, Object> list(
            @RequestParam(value = "content") String content,
            @RequestParam(value = "type") int type,  //1.主题查找；2.用户查找
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            if (Common.isWindows()) {
                content = Common.encodeStr(content);
            }
            if (type == 1) {
                List<EventsSearchVo> eventsSearchVoList = new ArrayList<EventsSearchVo>();
                List<String> nameList = eventsDao.getEventsNameByLikeName(content);
                for (String name : nameList) {
                    EventsSearchVo eventsSearchVo = new EventsSearchVo();
                    eventsSearchVo.setName(name);
                    List<Events> eventsList = eventsDao.getEventsByName(name);
                    if (eventsList.size() == 1) {
                        eventsSearchVo.setCreateTime(eventsList.get(0).getCreateTime());
                        eventsSearchVo.setId(eventsList.get(0).getId());
                        eventsSearchVo.setAddress(eventsList.get(0).getCreateAddress());
                        eventsSearchVo.setIsOnly(1);
                    } else {
                        eventsSearchVo.setId(0);
                        eventsSearchVo.setIsOnly(0);
                    }
                    eventsSearchVo.setCount(eventsList.size());
                    eventsSearchVoList.add(eventsSearchVo);
                }
                resultMap.put("eventsSearchVoList", eventsSearchVoList);
            } else if (type == 2) {

                List<StartsVo> startsVoList = new ArrayList<StartsVo>();
                List<AppUser> userList = userDao.getUserByName(content);

                for (AppUser user:userList){
                    StartsVo startsVo = new StartsVo();
                    long fansCount = focusDao.getFansByIdCount(user.getId());
                    user.setFansCount(fansCount);
                    startsVo.setAppUser(user);
                    Events newestEvents = eventsDao.getNewestEventsByUserId(user.getId());
                    startsVo.setNewestEvents(newestEvents);
                    startsVoList.add(startsVo);
                }
                if (startsVoList.size()==0){
                    resultMap.put("msg","您搜索的用户不存在");
                }else {
                    resultMap.put("startsVoList", startsVoList);
                }
            }

            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }


    /**
     * 点击进入查询结果
     * @param userId
     * @param name
     * @param request
     * @return
     */
    @RequestMapping(value = "/detail.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> detail(
            @RequestParam(value = "userId") long userId,
            @RequestParam(value = "name") String name,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            if (Common.isWindows()) {
                name = Common.encodeStr(name);
            }
            List<EventsVo1> resultList = new ArrayList<EventsVo1>();

            List<Events> eventsList= eventsDao.getEventsByName(name);
            for (Events events:eventsList){
                List<AppUser>   collectUserList = new ArrayList<AppUser>();

                EventsVo1 eventsVo1 = new EventsVo1();
                //活动id
                eventsVo1.setId(events.getId());
                //活动名
                eventsVo1.setName(events.getName());
                //活动图片
                EventComments firstEventComments = eventCommentsDao.getFirstEventComments(events.getId());
                List<Image> imageList = imageDao.getImageByEventCommentsId(firstEventComments.getId());
                eventsVo1.setImageList(imageList);
                eventsVo1.setEventCommentsId(firstEventComments.getId());
                //活动发布人信息
                eventsVo1.setAppUserId(events.getAppUserId());
                eventsVo1.setAppUser(userDao.getUserById(events.getAppUserId()));
                //活动描述
                eventsVo1.setDescription(events.getDescription());

                //评论
                List<Reply> replyList = replyDao.getReplyByEventCommentsId(firstEventComments.getId(), 0, 2);
                Collections.sort(replyList, new Comparator<Reply>() {
                    @Override
                    public int compare(Reply o1, Reply o2) {
                        return o1.getCreateTime().compareTo(o2.getCreateTime());
                    }
                });
                eventsVo1.setReplyList(replyList);
                //经纬度
                eventsVo1.setLatitude(events.getLatitude());
                eventsVo1.setLongitude(events.getLongitude());

                //创建时间
                eventsVo1.setCreateAddress(events.getCreateAddress());
                eventsVo1.setCreateTime(events.getCreateTime());
                eventsVo1.setUpdateTime(events.getUpdateTime());

                //点赞人
                List<Collect> collectList =  collectDao.getByEventsId(events.getId(), 0, 10);
                for (Collect collect:collectList){
                    AppUser appUser = userDao.getUserById(collect.getAppUserId());
                    if (appUser!=null){
                        collectUserList.add(appUser);
                    }
                }
                eventsVo1.setCollectUserList(collectUserList);
                eventsVo1.setCollectCount(collectDao.getByEventsIdCount(events.getId()));


                //是否能点赞
                if (userId != 0) {
                    if (collectDao.getByIds(userId,events.getId())==null) {
                        eventsVo1.setIsCollect(0);
                    } else {
                        eventsVo1.setIsCollect(1);
                    }
                } else {
                    eventsVo1.setIsCollect(1);
                }

                resultList.add(eventsVo1);
            }
            resultMap.put("resultList", resultList);
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }



    /**
     * 标签搜索
     *
     * @param name
     * @param request
     * @return
     */
    @RequestMapping(value = "/tagSearch.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> tagSearch(
            @RequestParam(value = "name") String name,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            if (Common.isWindows()) {
                name = Common.encodeStr(name);
            }
            List<Tag> tagList = tagDao.getTagByLikeName(name);
            List<String> tags = new ArrayList<String>();
            for (Tag tag : tagList) {
                tags.add(tag.getName());
            }
            resultMap.put("tags", tags);
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;

    }

}
